package service

import (
	"dsc-server/domain/dto"
	"dsc-server/pkg/model"
	"fmt"

	"gorm.io/gorm"
)

// UserService 处理用户相关的服务
type UserService struct {
	DB *gorm.DB
}

// NewUserService 创建UserService实例
func NewUserService(db *gorm.DB) *UserService {
	return &UserService{DB: db}
}

// GetUser 根据用户名和密码获取用户信息
func (s *UserService) GetUser(username string, password string) (*model.User, error) {
	var user model.User
	result := s.DB.Where("username = ? AND password = ? AND is_deleted = 0", username, password).First(&user)
	return &user, result.Error
}

// GetUserById 根据ID获取用户信息
func (s *UserService) GetUserById(id uint) (*model.User, error) {
	var user model.User
	result := s.DB.Where("id = ? AND is_deleted = 0", id).First(&user)
	return &user, result.Error
}

// ResetPassword 重置所有用户密码
func (s *UserService) ResetPassword(password string) dto.Result {
	if err := s.DB.Model(&model.User{}).Where("is_deleted = 0").Update("password", password).Error; err != nil {
		return dto.FailureMessage(nil, fmt.Sprintf("重置密码失败: %v", err))
	}

	return dto.SuccessMessage(nil, "重置密码成功")
}
